home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Various / DevDisk 65 (1989)(DevWare PD).zip / DevDisk 65 (1989)(DevWare PD).adf / 3d / cube.c < prev    next >
C/C++ Source or Header  |  1990-07-11  |  4KB  |  143 lines

  1. /* Cube Demo Program - By Steve Ludtke */
  2. /*     cre : 1/3/90    mod : 1/5/90    */
  3. /*           Copyright 1990            */
  4.  
  5. /*
  6.           The 3d library and all associated software in this distribution
  7.           is Copyright 1990 by Steven J. Ludtke. You have permission to
  8.           use and/or modify this code for any purpose commercial or non-
  9.           commercial with two conditions : I must be given credit in any
  10.           distributed product's documentation, and if any part of this
  11.           package is used in any commercial product (even Shareware) one
  12.           free copy of said software must be sent to me at the following
  13.           address : Steven Ludtke, 406 Yale Cir., Glenwood Springs, CO
  14.           81601; all other royalties are waived. This Copyright notice
  15.           must accompany any distributions of any part of this package,
  16.           and in general, the package should be distributed intact, with
  17.           no modifications. This notice must not be removed from the 3d.c,
  18.           test.c, and cube.c source code in this release.
  19. */
  20.  
  21. /* This program works almost exactly like test.c with 2 exceptions. First,
  22. This program reads from a predefined string in memory instead of the
  23. keyboard. The moves are rather grainy, so the animation isn't very smooth
  24. looking. The other difference is double buffering. This program opens
  25. two screens, then alternates which one it draws on. (It always draws on
  26. the hidden one, then brings it up front.) In general, look at the comments
  27. in test.c for more info. */
  28.  
  29.  
  30. #include<stdio.h>
  31.  
  32. #define D3VDIST 1000        /* distance to vanishing point */
  33. #define REZ     1024        /* calc resolution must be 2^x */
  34. #define REZB    7           /* number of bits in REZ */
  35. #define XCEN    320         /* center of bitmap X */
  36. #define YCEN    90          /* center of bitmap Y */
  37. #define XHI     639
  38. #define YHI     189
  39. #define XLO     0
  40. #define YLO     0
  41. #define ASPECT  22/10       /* aspect ratio */
  42. #include "3d.c"
  43.  
  44. #define NUMVEC 10
  45.  
  46. APTR IntuitionBase,GfxBase;
  47.  
  48. struct NewScreen NS = { 0,0,640,200,2,1,0,HIRES,CUSTOMSCREEN,NULL,"cube",NULL,
  49.     NULL };
  50.  
  51. long xxx[NUMVEC] = { 1, 1, 1, 1,-1,-1,-1,-1 };
  52. long yyy[NUMVEC] = { 1, 1,-1,-1, 1, 1,-1,-1 };
  53. long zzz[NUMVEC] = { 1,-1, 1,-1, 1,-1, 1,-1 };
  54.  
  55. LINES line[20] = { {0,1,0,0},{1,0,0,0},{5,0,0,0},{4,0,0,0},{0,0,0,0},
  56.     {0,1,0,0},{4,0,0,0},{6,0,0,0},{2,0,0,0},{0,0,0,0},
  57.     {2,1,0,0},{6,0,0,0},{7,0,0,0},{3,0,0,0},{2,0,0,0},
  58.     {7,1,0,0},{3,0,0,0},{1,0,0,0},{5,0,0,0},{7,0,0,0} };
  59.  
  60. char *todo = "88888888888888886kikikiki6666663339369666jjjjmmmmpp5333333333333333333333333333533333333333333333333333335666636363636363693996969696ppppq";
  61.  
  62. int pt = 0;
  63.  
  64. nextc()
  65. {
  66. return(todo[pt++]);
  67. }
  68.  
  69. main()
  70. {
  71. VECTOR v;
  72. double a1,a2,a3;
  73. char c;
  74. int i,p,q,r,f,crp;
  75. struct RastPort *rp[2];
  76. struct Screen *s1,*s2;
  77.  
  78. crp=f=0;
  79. GfxBase=(APTR) OpenLibrary("graphics.library",0);
  80. IntuitionBase=(APTR) OpenLibrary("intuition.library",0);
  81.  
  82. s1=OpenScreen(&NS);
  83. s2=OpenScreen(&NS);
  84. if (s1==NULL || s2==NULL) { printf("Can't open screen(s)\n"); exit(0); }
  85. rp[0]=&s1->RastPort;
  86. rp[1]=&s2->RastPort;
  87.  
  88. v.x=xxx;
  89. v.y=yyy;
  90. v.z=zzz;
  91. v.tx=(long *) malloc(NUMVEC*sizeof(long));
  92. v.ty=(long *) malloc(NUMVEC*sizeof(long));
  93. v.tz=(long *) malloc(NUMVEC*sizeof(long));
  94.  
  95. for(i=0; i<8; i++) { xxx[i]*=40; yyy[i]*=40; zzz[i]*=40; }
  96.  
  97. a1=a2=a3=0;
  98. p=q=r=0;
  99. Init3Ras(rp[0],rp[1]);
  100. SetAPen(rp[0],1);
  101. SetOPen(rp[0],2);
  102. SetDrMd(rp[0],JAM1);
  103. SetAPen(rp[1],1);
  104. SetOPen(rp[1],2);
  105. SetDrMd(rp[1],JAM1);
  106.  
  107. while ((c=nextc())!='q') {
  108. switch(c) {
  109. case '9': a1+=PI/20; break;
  110. case '7': a1-=PI/20; break;
  111. case '6': a2+=PI/20; break;
  112. case '4': a2-=PI/20; break;
  113. case '3': a3+=PI/20; break;
  114. case '1': a3-=PI/20; break;
  115. case '5': f^=1;      break;
  116. case 'k': q+=5;     break;
  117. case 'j': q-=5;     break;
  118. case 'm': r+=5;     break;
  119. case 'i': r-=5;     break;
  120. case '8': p+=10;     break;
  121. case '2': p-=10;     break;
  122. case 'p': for (i=0; i<30; i++) WaitTOF(); break;
  123. }
  124. setxfm(a1,a2,a3,q,p,r,1,1);
  125. rotatev(&v,8);
  126. SetRast(rp[crp],0);
  127. if (f==0) d3lines(&v,line,20,rp[crp]);
  128. else d3surf(&v,line,20,rp[crp]);
  129. if (crp==1) ScreenToFront(s2);
  130. else ScreenToFront(s1);
  131. crp^=1;
  132. }
  133.  
  134. Exit3d(rp[0]);
  135. CloseScreen(s1);
  136. CloseScreen(s2);
  137. free(v.tx);
  138. free(v.ty);
  139. free(v.tz);
  140. }
  141.  
  142.  
  143.